﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace TPCN.DAL
{
   public class DonDatHangDAL
    {
       public static DonDatHangObj GetByID(string DondathangID)
       {
           DonDatHangObj obj = new DonDatHangObj();
           using (SqlCommand cmd =new SqlCommand())
           {
               cmd.CommandText="sp_tbDonDatHang_GetByID";
               cmd.CommandType= CommandType.StoredProcedure;
               cmd.Parameters.AddWithValue("@DondathangID",DondathangID);
               using (SqlDataReader dr = Database.db.GetDataToDataReader(cmd))
                {
                    if (dr.HasRows)
                    {
                        dr.Read();
                        obj = obj.SetValuesFromDataReader(dr);
                    }
                    dr.Close();
                }
           }
           return obj;
       }

       public static List<DonDatHangObj> GetNonComfirmOrders()
       {
           List<DonDatHangObj> result = new List<DonDatHangObj>();
           DonDatHangObj obj = new DonDatHangObj();
           using (SqlCommand cmd = new SqlCommand())
           {
               cmd.CommandText = "sp_tbDonDatHang_GetNonComfirmOrders";
               cmd.CommandType = CommandType.StoredProcedure;
               using (SqlDataReader dr = Database.db.GetDataToDataReader(cmd))
               {
                   if (dr.HasRows)
                   {
                       while (dr.Read())
                       {
                           result.Add(obj.SetValuesFromDataReader(dr));
                       }
                   }
                   dr.Close();
                   obj = null;
               }
           }
           return result;
       }

       public static List<DonDatHangObj> GetByAll()
       { 
           List<DonDatHangObj> result =new List<DonDatHangObj>();
           DonDatHangObj obj = new DonDatHangObj();
           using (SqlCommand cmd = new SqlCommand())
           {
               cmd.CommandText="sp_tbDonDatHang_GetByAll";
               cmd.CommandType =CommandType.StoredProcedure;
               using (SqlDataReader dr = Database.db.GetDataToDataReader(cmd))
                {
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            result.Add(obj.SetValuesFromDataReader(dr));
                        }
                    }
                    dr.Close();
                    obj = null;
                }
           }
           return result;
       }

       public static List<DonDatHangObj> GetByKhachID(string KhachID)
       {
           List<DonDatHangObj> result = new List<DonDatHangObj>();
           DonDatHangObj obj = new DonDatHangObj();
           using (SqlCommand cmd = new SqlCommand())
           {
               cmd.CommandText = "sp_tbDonDatHang_GetByKhachID";
               cmd.CommandType = CommandType.StoredProcedure;
               cmd.Parameters.Add("@KhachID", KhachID);
               using (SqlDataReader dr = Database.db.GetDataToDataReader(cmd))
               {
                   if (dr.HasRows)
                   {
                       while (dr.Read())
                       {
                           result.Add(obj.SetValuesFromDataReader(dr));
                       }
                   }
                   dr.Close();
                   obj = null;
               }
           }
           return result;
       }

       public static int Insert(DonDatHangObj obj)
       {
           int value = 0;
           try
           {
               using (SqlCommand cmd = new SqlCommand())
               {
                   cmd.CommandText = "sp_tbDonDatHang_Insert";
                   cmd.CommandType = CommandType.StoredProcedure;
                   cmd.Parameters.AddWithValue("@Ngaylap", (obj.Ngaylap == null) ? String.Empty : obj.Ngaylap);
                   cmd.Parameters.AddWithValue("@KhachID", (obj.KhachID == null) ? String.Empty : obj.KhachID);
                   cmd.Parameters.AddWithValue("@PhuongthucthanhtoanID", (obj.PhuongthucthanhtoanID == null) ? String.Empty : obj.PhuongthucthanhtoanID);
                   cmd.Parameters.AddWithValue("@Active", (obj.Active == null) ? String.Empty : obj.Active);
                   cmd.Parameters.AddWithValue("@Hotennguoinhan", (obj.Hotennguoinhan == null) ? String.Empty : obj.Hotennguoinhan);
                   cmd.Parameters.AddWithValue("@Sodienthoai", (obj.Sodienthoai == null) ? String.Empty : obj.Sodienthoai);
                   cmd.Parameters.AddWithValue("@Diachinhanhang", (obj.Diachinhanhang == null) ? String.Empty : obj.Diachinhanhang);
                   cmd.Parameters.AddWithValue("@Ghichu", (obj.Ghichu == null) ? String.Empty : obj.Ghichu);
                   cmd.Parameters.AddWithValue("@Hinhthucnhanhang", (obj.Hinhthucnhanhang == null) ? String.Empty : obj.Hinhthucnhanhang);

                   SqlParameter id = new SqlParameter("@ID", DbType.Int32);
                   id.Direction = ParameterDirection.Output;
                   cmd.Parameters.Add(id);

                   Database.db.ExecuteNonQuery(cmd);

                   value = Int32.Parse(cmd.Parameters["@ID"].Value.ToString());
               }
               return value;
           }
           catch (Exception)
           {
               return 0;
           }
           finally
           {

           }
       }
       public static bool Update(DonDatHangObj obj)
       {
           try
           {
               using (SqlCommand cmd = new SqlCommand())
               {
                   cmd.CommandText = "sp_tbDonDatHang_Update";
                   cmd.CommandType = CommandType.StoredProcedure;
                   cmd.Parameters.AddWithValue("@DondathangID" ,(obj.DondathangID == null) ? String.Empty:obj.DondathangID);
                   cmd.Parameters.AddWithValue("@Ngaylap", (obj.Ngaylap == null) ? String.Empty : obj.Ngaylap);
                   cmd.Parameters.AddWithValue("@KhachID", (obj.KhachID == null) ? String.Empty : obj.KhachID);
                   cmd.Parameters.AddWithValue("@PhuongthucthanhtoanID", (obj.PhuongthucthanhtoanID == null) ? String.Empty : obj.PhuongthucthanhtoanID);
                   cmd.Parameters.AddWithValue("@Active", (obj.Active == null) ? String.Empty : obj.Active);
                   cmd.Parameters.AddWithValue("@Hotennguoinhan", (obj.Hotennguoinhan == null) ? String.Empty : obj.Hotennguoinhan);
                   cmd.Parameters.AddWithValue("@Sodienthoai", (obj.Sodienthoai == null) ? String.Empty : obj.Sodienthoai);
                   cmd.Parameters.AddWithValue("@Diachinhanhang", (obj.Diachinhanhang == null) ? String.Empty : obj.Diachinhanhang);
                   cmd.Parameters.AddWithValue("@Ghichu", (obj.Ghichu == null) ? String.Empty : obj.Ghichu);
                   cmd.Parameters.AddWithValue("@Hinhthucnhanhang", (obj.Hinhthucnhanhang == null) ? String.Empty : obj.Hinhthucnhanhang);
                   Database.db.ExecuteNonQuery(cmd);
               }
               return true;
           }
           catch (Exception)
           {
               return false;
           }
       }
       public static bool Delete(string DondathangID)
       {
           try
           {
               using (SqlCommand cmd = new SqlCommand())
               {
                   cmd.CommandText = "sp_tbDonDatHang_DeleteByID";
                   cmd.CommandType = CommandType.StoredProcedure;
                   cmd.Parameters.AddWithValue("@DondathangID", DondathangID);
                   Database.db.ExecuteNonQuery(cmd);
               }
               return true;
           }
           catch(Exception)
           {
               return false;
           }
       }

    }
}
